import {getDriverAllocation,sendDistribution} from "../../../../network/client"
import {
  toast
} from '../../../../utils/Tools'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showIndex:'0',
    // 信息
    driverInfo:"",
    // 吨数
    product_num:'',
    //页面参数
    PageParams:"",
    improveId:0,
   
  },
  
onReady:function(){
  this.affirm=this.selectComponent("#affirm")
},
showAffirm(){
  this.affirm.showAffirm()
},
//  咨询框 send//
async consultPop(e) {
  let consult = e.currentTarget.dataset['consult']
  if (consult === "true") {
    // this._sendSelectState()
  
  } else {
 
    toast("已取消")
  }
  this.setData({
    affirm: true,
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  
  onLoad: function (options) {
   
    this._getDriverAllocation(options.detail)
    this.setData({
      PageParams:options.detail
    })
  },
  showBtn(e){
    console.log(e)
    const index=e.currentTarget.dataset.index
    let improveId=e.currentTarget.dataset.id;
    let driverInfo=this.data.driverInfo;
    let driverIndex=e.currentTarget.dataset.indexone;
    for(var key in driverInfo){
      if(key===driverIndex){
        driverInfo[key][index].checked=!driverInfo[key][index].checked;
      }
    }
    this.setData({
      driverInfo
    })
    this.setData({
      showIndex:index,
      improveId,
    })
  
  },
  // 分配到某个司机
  async _sendDistribution(e){

    let driverId=e.currentTarget.dataset["driverid"]
    const data={
      order_id:this.data.PageParams,
      driver_id:driverId,
      ton:this.data.product_num
    }
    const res=await sendDistribution(data)
    console.log(res)
    if (res.code === 200) {
      toast('操作成功', 'success')
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/order/order/order',
        })
      }, 1500);
      
    } else {
      toast(res.message)
    }
  },

  // 数据请求//
  async _getDriverAllocation(params){
    const data={
      order_id:params,
      edit_name:"distribution"
    }
    const res=await getDriverAllocation(data)
    if(res.code===200){
     let aa=res.data.driver;
     for(var key in aa){
       for(var keys in aa[key]){
         aa[key][keys].checked=true
       }
     }
     console.log(res)
      this.setData({
        driverInfo:res.data.driver,
        product_num:res.data.product_num
      })
    }
  }
})